package com.dreampharos.util;

import com.dreampharos.util.Config;

public class Paging {

	public String page(String totalCnt, String nowPage, String pre_img, String next_img) {
		String str       = "";
		String returnStr = "";
		
		int ntotalCnt = Integer.parseInt(totalCnt);
		int nlistCnt  = Integer.parseInt(Config.LIST_CNT);
		int nskipPage = Integer.parseInt(Config.SKIP_PAGE);
		int nnowPage  = Integer.parseInt(nowPage);

		/*endPage*/
		int endPage = (nnowPage + nskipPage) / nskipPage;
		
		if(((nnowPage + nskipPage) % nskipPage) == 0) {
			endPage = nnowPage;
		} else {
			endPage = endPage * nskipPage;
		}
		
		/*starPage*/
		int starPage = (endPage - nskipPage) + 1;
		
		/*totalPage Count*/
		int totalpage = ntotalCnt / nlistCnt;
		if((ntotalCnt % nlistCnt) != 0) {
			++totalpage;			
		}

		if("".equals(pre_img) || "".equals(next_img)) {
			pre_img  = "<<";
			next_img = ">>";
		} else {
			pre_img  = "<img src='" + pre_img + "' width='8' height='7' border='0' align='absmiddle'>";
			next_img = "<img src='" + next_img + "' width='8' height='7' border='0' align='absmiddle'>";
		}
		
		if(nnowPage > nskipPage) {
			str = str + " <a href='javascript:fncPage("+(starPage-nskipPage)+")'>"+ pre_img +"</a>";
		} else {
			str = str + pre_img;
		}
		
		while(starPage <= endPage) {
			if(starPage > totalpage) {
				break;
			} else if(starPage == nnowPage){
				str = str + " <b><font color='#3e03d6'>["+starPage+"]</font></b>";
				starPage++;
			} else {
				str = str + " <a href='javascript:fncPage("+starPage+")'>["+starPage+"]</a>";
				starPage++;
			}
		}
		
		if(endPage < totalpage) {
			str = str + " <a href='javascript:fncPage("+(endPage+1)+")'>" + next_img + "</a>";
		} else {
			str = str + " " + next_img;
		}
		
		returnStr = "";
		returnStr = returnStr + "<table border='0' cellpadding='0' cellspacing='0' width='100%'>";
		returnStr = returnStr + "  <tr>";
		returnStr = returnStr + "    <td align='center'>" + str + "</td>";
		returnStr = returnStr + "  </tr>";
		returnStr = returnStr + "</table>";		

		return returnStr;		
	}
	
}
